<%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>渠道池管理</title>
<%@ include file="/pages/common/header.jsp" %>
<script type="text/javascript">
//渠道状态控制
var stateData = [{ state: 1, text: '启动' }, { state: 0, text: '停止'}];
var isEdit = false;
var editUrlDiv;
var speedGrades = new Array();
<s:iterator value="vo.speedGrades" var="grade" status="ind">
	speedGrades[${ind.index}] = "${grade } /ms";
</s:iterator>
//表格列数据
var columns=[
	{display:"序号",width:80,render:function(rowData,rowIndex){
		return (parseInt(this.get("page"))-1)*parseInt(this.get("pageSize"))+rowIndex+1;
	}},
	{display:"id",name:"id",editor: { type: 'text' }},
	{display:"编码",width:80,name:"code",render: function (item,rowIndex){
       return "<div id='rowid_"+rowIndex+"'>"+item.code+"</div>";
    }},
	{display:"渠道名称",width:130,name:"name"},
	{display:"创建人名称",name:"createName",width:130},
	{display:"创建时间",name:"createTime",width:130},
	{display:"URL地址",name:"validDefaultUrl",width:130},
	{display:"最后更新时间",name:"lastTime",width:130},
	{display:"ip倍数",name:"times",width:130},
	{display:"验证方式",name:"isValid",width:130,type: 'int',
		render: function (item)
        {
        switch(parseInt(item.isValid)) {
        case 0:return '不验证';
        case 1:return '验证';
        case 2:return '定制验证';
        case 3:return 'ping方式';
        }

    }},
    {display:"ip最低速度",name:"speedGrade",width:130,
    	 render: function (item)
         {
    		return speedGrades[parseInt(item.speedGrade)-1];
         }
    },
	{display:"状态",name:"state",width:130,type: 'int',
        render: function (item)
        {
            if (parseInt(item.state) == 1)
            	{
            	    return "<input type='button' id='Button_"+item.code+"'_state='"+item.state+"' value='停止' style='background-color:#00FFFF' onclick='stopState("+item.code+","+item.id+")'+/>";
            	}
   
                 return "<input type='button'id='Button_"+item.code+"' _state='"+item.state+"' value='启动' style='background-color:#CCCCCC' onclick='startState("+item.code+","+item.id+")'+/>";
        }
   
	}
];
var toolbarOptions = {
		items : [ {
			id : "add",
			text : '增加',
			click : toolbarBtnItemClick,
			img : "../../images/icons/toolbar/add.png"
		}, {
			line : true
		}, {
			id : "modify",
			text : '修改',
			click : toolbarBtnItemClick,
			img : "../../images/icons/toolbar/page_edit.gif"
		}, {
			line : true
		}, {
			id : "remove",
			text : '删除',
			click : toolbarBtnItemClick,
			img : "../../images/icons/toolbar/page_delete.gif"
		} ]
	};

$(function(){
	initGrid();
	
});

function initGrid(){
	var url="<%=request.getContextPath()%>/operate/channelPoolManager/search.action";
	var g=$("#ressult_div").ligerGrid({
		height:'100%',
		width:'100%',
		columns:columns,
		enabledSort:false,
		pageSize:50,
		toolbar : toolbarOptions,
		root:"result",//数据集合
		record:"dataSize",//总数据数
		pageParmName:"pageIndex",//页索引
		pagesizeParmName:"pageLen",//页长
		usePager:true,
		dataAction:"local",
		method:"post",
		delayLoad:false,//初始化不加载
		enabledEdit:true,
		checkbox : true,
		pageSizeOptions:[10,20,50,200],
		url:url,
	    onSelectRow: function (rowdata, rowindex)
         {
             $("#txtrowindex").val(rowindex);
         }
	     
	
	});
	var manager = $("#ressult_div").ligerGetGridManager(); 
	manager.set("height","100%");
	g.toggleCol("id",false);
    return g;
}

//工具条事件
function toolbarBtnItemClick(item) {
	switch (item.id) {
	case "add":
	    isEdit = false;
		editUrlDiv = $.ligerDialog.open({
			title : "增加渠道",
			isResize : true,
			width : 430,
			height : 350,
			isHidden : true,
			buttons : [ {
				id : 'save',
				text : '保存',
				onclick : btnClick
			}, {
				id : 'cancel',
				text : '取消',
				onclick : btnClick
			} ],
			target : $("#editUrl")
		});
		break;
	case "modify":
		var manager=$("#ressult_div").ligerGetGridManager();
		var rows = manager.getCheckedRows();
		if (rows.length > 1) {
			$.ligerDialog.alert('只能选中一行!');
			return;
		}
		if (rows.length == 0) {
			$.ligerDialog.alert('请选择行!');
			return;
		}
		selectedRow = rows[0];
		isEdit = true;
		$("#name").val(manager.getSelected().name);
		$("#validDefaultUrl").val(manager.getSelected().validDefaultUrl);
		$("#channelcode").val(manager.getSelected().code);
		/* $("#password").val(""); */
		$("#times").val(manager.getSelected().times);
		$("#isValid").val(manager.getSelected().isValid);
		$("#speedGrade").val(manager.getSelected().speedGrade);
		
		editUrlDiv = $.ligerDialog.open({
			title : "修改渠道",
			isResize : true,
			width : 430,
			height : 350,
			isHidden : true,
			buttons : [ {
				id : 'save',
				text : '保存',
				onclick : btnClick
			}, {
				id : 'cancel',
				text : '取消',
				onclick : btnClick
			} ],
			target : $("#editUrl")
		});
		break;
	case "remove":
		var manager = $("#ressult_div").ligerGetGridManager();
		selectedRow = manager.getSelectedRow();
		if (!selectedRow) {
			$.ligerDialog.alert('请选择行!');
			return;
		}
		jQuery.ligerDialog.confirm('确定删除吗?', function(confirm) {
			if (confirm) {
				f_delete();
			}
		});
		break;
	}
}
function btnClick(button) {
	if (button.id == "save") {
	//	if(checkChannel())
    //      {
	//		    alert("渠道编码可用");
				saveUrlResource();
	//       }
	//	else{
	//		alert("渠道编码已经存在，不可以重复，请正确输入信息！！");
	//		return;
	//	}
		
	} else if (button.id == "cancel") {
		editUrlDiv.hidden();
	}
}

function saveUrlResource() {
	    var name=$("#name").val();
	    var validDefaultUrl=$("#validDefaultUrl").val();
	    var channelcode=$("#channelcode").val();
	    /* var password=$("#password").val(); */
	    var isValid=$("#isValid").val();
	    var speedGrade=$("#speedGrade").val();
	    var times=$("#times").val();
	    
	    if(""==name) {
	    	     $("#name").ligerTip({content:"字段不能为空",
	    	    		              callback:function()
					                         {
					                             setTimeout(function ()
					
					                             {
					
					                                 $("#name").ligerHideTip(); //5秒延迟后关闭tip
					
					                             }, 5000);
					                         }

                         }) ;return;
	    	}
	    if(""==validDefaultUrl) {
	    	     $("#validDefaultUrl").ligerTip({content:"字段不能为空",
							    	    	 callback:function()
						                     {
						                         setTimeout(function ()
						
						                         {
						
						                             $("#validDefaultUrl").ligerHideTip(); //5秒延迟后关闭tip
						
						                         }, 5000);
						                     }
	    	    	 	}) ;return;
	    	}
	    //编码验证
	    if(""==channelcode) {
	    	     $("#channelcode").ligerTip({content:"字段不能为空",
							    	    	 callback:function()
						                     {
						                         setTimeout(function ()
						
						                         {
						
						                             $("#channelcode").ligerHideTip(); //5秒延迟后关闭tip
						
						                         }, 5000);
						                     }
	    	    	   }) ;return;
	    	}
	    //验证
	   /*   if(""==password) {
	    	     $("#password").ligerTip({content:"字段不能为空",
							    	    	 callback:function()
						                     {
						                         setTimeout(function ()
						
						                         {
						
						                             $("#password").ligerHideTip(); //5秒延迟后关闭tip
						
						                         }, 5000);
						                     }
	    	    	   }) ;return;
	    	} */
	    if(""==isValid) {
	    	     $("#isValid").ligerTip({content:"字段不能为空",
							    	    	 callback:function()
						                     {
						                         setTimeout(function ()
						
						                         {
						
						                             $("#isValid").ligerHideTip(); //5秒延迟后关闭tip
						
						                         }, 5000);
						                     }
	    	    	   }) ;return;
	    	}
			    if(isEdit)
			    {
				    	var id=$("#ressult_div").ligerGetGridManager().getSelected().id;
					    $.ajax({
					    	type:"POST",
					    	url:"<%=request.getContextPath()%>/operate/channelPoolManager/updateChannel.action",
					    	data:{
					    		"vo.id":id,
					    		"vo.name":name,
					    		"vo.validDefaultUrl":validDefaultUrl,
					    		"vo.code":channelcode,
					    		//"vo.password":password,
					    		"vo.speedGrade":speedGrade,
					    		"vo.times":times,
					    		"vo.isValid":isValid
					    	},
					    	loading : "正在保存中...",
					    	success:function(data){
					    		$.ligerDialog.success("保存成功", '提示信息');
					    		$("#ressult_div").ligerGetGridManager().loadData();
						},
					 	error:function(){
					 		alert("服务器异常，请稍后再试！");
					 		isSuccess = false;
					 	}	
					    });
					    editUrlDiv.hidden();
			    	
	          }else
	        	  {
		        	  $.ajax({
					    	type:"POST",
					    	url:"<%=request.getContextPath()%>/operate/channelPoolManager/addChannel.action",
					    	data:{
					    		"vo.name":name,
					    		"vo.validDefaultUrl":validDefaultUrl,
					    		"vo.code":channelcode,
					    		/* "vo.password":password, */
					    		"vo.speedGrade":speedGrade,
					    		"vo.times":times,
					    		"vo.isValid":isValid
					    	},
					    	loading : "正在保存中...",
					    	success:function(data){
					    		$.ligerDialog.success("保存成功", '提示信息');
					    		$("#ressult_div").ligerGetGridManager().loadData();
						},
					 	error:function(){
					 		alert("服务器异常，请稍后再试！");
					 		isSuccess = false;
					 	}	
					    });
		        	  editUrlDiv.hidden();
	        	  }
	          
}

function stopState(code,id){
			var manager=$("#ressult_div").ligerGetGridManager();
	        document.getElementById("Button_"+code).value="启动";
	        document.getElementById("Button_"+code).style.backgroundColor='#CCCCCC';
		    var currentcode=code;
		    var currentstate=$("#Button_"+code).attr("_state");
		    
		    var url="<%=request.getContextPath()%>/operate/channelPoolManager/stopState.action";
		    $.ajax({
		    	type:"POST",
		    	url:url,
		    	data:{
		    		"vo.id":id,
		    		"vo.code":currentcode,
		    		"vo.state":currentstate
		    	},
		    	loading : "正在停止中...",
		    	success:function(data){
		    		$("#Button_"+code).attr("_state",0);
		    		$.ligerDialog.success("操作成功", '提示信息');
		    		manager.loadData();
				},
			 	error:function(){
			 		alert("服务器异常，请稍后再试！");
			 		isSuccess = false;
			 	}	
		    });
	    }
function startState(code,id){
	var manager=$("#ressult_div").ligerGetGridManager();
	document.getElementById("Button_"+code).value="停止";
	document.getElementById("Button_"+code).style.backgroundColor='#00FFFF';
    var currentcode=code;
    var currentstate=$("#Button_"+code).attr("_state");
    
    var url="<%=request.getContextPath()%>/operate/channelPoolManager/startState.action";
    $.ajax({
    	type:"POST",
    	url:url,
    	data:{
    		"vo.id":id,
    		"vo.code":currentcode,
    		"vo.state":currentstate
    	},
    	loading : "正在启动中...",
    	success:function(data){
    		$("#Button_"+code).attr("_state",1);
    		$.ligerDialog.success("操作成功", '提示信息');
    		manager.loadData();
		},
	 	error:function(){
	 		alert("服务器异常，请稍后再试！");
	 		isSuccess = false;
	 	}	
    });
}


function f_delete() {
	var manager = $("#ressult_div").ligerGetGridManager();
	if (manager.getCheckedRows().length == 0) {
		$.ligerDialog.alert('请选择行!');
		return;
	}
	
	if (manager.getCheckedRows().length > 1) {
		$.ligerDialog.alert('只能选中一行!');
		return;
	}
	 var id =manager.getSelected().id;
	 var channelcode=manager.getSelected().code;
	 var url="<%=request.getContextPath()%>/operate/channelPoolManager/deleteChannel.action";
	$.ajax({
    	type:"POST",
    	url:url,
    	data:{
    		"vo.id":id,
    		"vo.code":channelcode
    	},
    	loading : "正在删除中...",
    	success:function(data){
    		$.ligerDialog.success("操作成功", '提示信息');
    		$("#ressult_div").ligerGetGridManager().loadData();
		},
	 	error:function(){
	 		alert("服务器异常，请稍后再试！");
	 		isSuccess = false;
	 	}	
    });
}
//判断输入的渠道编码在数据库是否存在
function checkChannel(){
	var channelcode=$("#channelcode").val();
	 var url="<%=request.getContextPath()%>/operate/channelPoolManager/checkChannel.action";
		$.ajax({
	    	type:"POST",
	    	url:url,
	    	dataType:'json',
	    	data:{
	    		"vo.code":channelcode
	    	},
	    	loading : "正在加载...",
	    	success:function(data){
	    		if(data)
	    		    {alert("编码已存在，不可用");
	    		    }
	    		else
	    			{
	    			 alert("编码可用");
	    			}
			},
		 	error:function(){
		 		alert("服务器异常，请稍后再试！");
		 		isSuccess = false;
		 	}	
	    });

}
</script>
</head>
<body>

<div class="main"  style="height:100%">
	
	<div style="height:25px;border:0px;"></div>
	<div id="ressult_div" style="height:100%"><span/></div>
	 
	 <div id="editUrl" style="display: none;">
	  <form id="channel_form">
	  	<table class="form2column">
	  		
	  		<tr>
	  			<td class="label">渠道名称</td>
	  			<td class="content"><input type="text" id="name" name="lname"  /></td>
	  		</tr>
	  		
	  		<tr>
	  			<td class="label">URL地址</td>
	  			<td class="content"><input type="text" id="validDefaultUrl" name="lvalidDefasultUrl"  /></td>
	  		</tr>
	  		<tr>
	  			<td class="label">渠道编码</td>
	  			<td class="content"><input type="text" id="channelcode" name="lchannelcode" onblur="checkChannel()"/></td> 
	  		</tr>
	  	<!-- 	<tr>
	  			<td class="label">密码</td>
	  			<td class="content"><input type="text" id="password" name="lpassword" /></td>
	  		</tr> -->
	  		<tr>
	  			<td class="label">ip倍数</td>
	  			<td class="content"><input type="text" id="times" name="times" /></td>
	  		</tr>
	  		<tr>
	  			<td class="label">验证方式</td>
	  			<td class="content">
	  				<select style="width:155px;height:25px" id="isValid" name="lisValid">
	  					<option value="0">不验证</option>
	  					<option value="1">验证</option>
	  					<option value="2">定制验证</option>
	  					<option value="3">ping方式</option>
	  				</select>
	  			</td>
	  		</tr>
	  		<tr>
	  			<td class="label">接受最低速度ip</td>
	  			<td class="content">
	  				<select style="width:155px;height:25px" id="speedGrade" name="speedGrade" >
		  				<s:iterator value="vo.speedGrades" var="grade" status="ind">
		  					<option ${ind.index+1==vo.speedGrade?selected:"" } value="${ind.index+1 }">${grade } b/ms</option>
		  				</s:iterator>
	  				</select>
	  			</td>
	  		</tr>
	  	</table>
	  </form>
  </div>
  
</div>
</body>
</html>